/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI;

import Interfaces.ApparentModalI;
import Model.Playlist;
import java.awt.Frame;
import java.awt.Image;
import javax.imageio.ImageIO;
import javax.persistence.EntityManager;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import src.ApparentModalC;
import src.General;


public class Playlists extends javax.swing.JFrame implements ApparentModalI
{

    private RadioStationJFrame prFrmRadioStation;
    private MngPlaylist frmMngPlaylist;
    private MngPlaylistXML frmMngPlaylistXML;
    private String username;
    private EntityManager em;
    private Playlist selectedPlaylist;
    private int selectedRow;
    private JFrame next;
    
    /*
     * Creates new form Playlists
     */
    public Playlists(RadioStationJFrame frmRadioStation, EntityManager em, String username) 
    {
        
        try 
        {
            this.prFrmRadioStation = frmRadioStation;
            this.username = username;
            this.em = em;
            
            initComponents();
            
            // Disable buttons if no entries exists
            General.disableDeleteUpdateIf(this.playlistTable, this.btnDeletion, this.btnModification);
            
            Image applicationImage = ImageIO.read(getClass().getResource("/Images/note1.png"));
            this.setIconImage(applicationImage);
        } 
        catch (Exception ex) 
        {
            JOptionPane.showMessageDialog(this, ex.getMessage(), this.getTitle(), JOptionPane.ERROR_MESSAGE);
        }
    }

    public void HandleButtons()
    {
        try 
        {
            General.enableDeleteUpdateIf(this.playlistTable, this.btnDeletion, this.btnModification);
            General.disableDeleteUpdateIf(this.playlistTable, this.btnDeletion, this.btnModification);
        } 
        catch (Exception ex) 
        {
            // Do nothing, swallow error
        }

    }
    
    
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

        playlistQuery = java.beans.Beans.isDesignTime() ? null : em.createQuery("SELECT p FROM Playlist p ORDER BY p.playlistName");
        playlistList = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : org.jdesktop.observablecollections.ObservableCollections.observableList(playlistQuery.getResultList());
        btnExit = new javax.swing.JButton();
        jSeparator1 = new javax.swing.JSeparator();
        btnCreation = new javax.swing.JButton();
        btnModification = new javax.swing.JButton();
        btnDeletion = new javax.swing.JButton();
        jSeparator2 = new javax.swing.JSeparator();
        lblActionButtons = new javax.swing.JLabel();
        lblPlaylists = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        playlistTable = new javax.swing.JTable();
        btnExportXML = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Radio Station - Λίστες Τραγουδιών");
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        btnExit.setText("Έξοδος");
        btnExit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnExitActionPerformed(evt);
            }
        });

        btnCreation.setText("Δημιουργία");
        btnCreation.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCreationActionPerformed(evt);
            }
        });

        btnModification.setText("Τροποποίηση");
        btnModification.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnModificationActionPerformed(evt);
            }
        });

        btnDeletion.setText("Διαγραφή");
        btnDeletion.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDeletionActionPerformed(evt);
            }
        });

        lblActionButtons.setText("Διαχείριση λίστας τραγουδιών");

        lblPlaylists.setText("Λίστες τραγουδιών");

        playlistTable.setAutoCreateRowSorter(true);
        playlistTable.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);

        org.jdesktop.swingbinding.JTableBinding jTableBinding = org.jdesktop.swingbinding.SwingBindings.createJTableBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, playlistList, playlistTable);
        org.jdesktop.swingbinding.JTableBinding.ColumnBinding columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${playlistName}"));
        columnBinding.setColumnName("Όνομα Λίστας");
        columnBinding.setColumnClass(String.class);
        columnBinding.setEditable(false);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${description}"));
        columnBinding.setColumnName("Περιγραφή");
        columnBinding.setColumnClass(String.class);
        columnBinding.setEditable(false);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${createdDate}"));
        columnBinding.setColumnName("Ημερομηνία Δημιουργίας");
        columnBinding.setColumnClass(java.util.Date.class);
        columnBinding.setEditable(false);
        bindingGroup.addBinding(jTableBinding);
        jTableBinding.bind();
        jScrollPane1.setViewportView(playlistTable);
        if (playlistTable.getColumnModel().getColumnCount() > 0) {
            playlistTable.getColumnModel().getColumn(0).setMinWidth(100);
            playlistTable.getColumnModel().getColumn(0).setPreferredWidth(180);
            playlistTable.getColumnModel().getColumn(1).setMinWidth(240);
            playlistTable.getColumnModel().getColumn(1).setPreferredWidth(240);
            playlistTable.getColumnModel().getColumn(2).setMinWidth(100);
            playlistTable.getColumnModel().getColumn(2).setPreferredWidth(180);
        }

        btnExportXML.setText("Εξαγωγή Λίστας σε XML");
        btnExportXML.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnExportXMLActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jSeparator1, javax.swing.GroupLayout.Alignment.TRAILING)
            .addComponent(jSeparator2, javax.swing.GroupLayout.Alignment.TRAILING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(0, 0, Short.MAX_VALUE)
                .addComponent(btnExit))
            .addGroup(layout.createSequentialGroup()
                .addComponent(btnCreation)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnModification)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnDeletion)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(btnExportXML))
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lblPlaylists)
                    .addComponent(lblActionButtons))
                .addContainerGap(650, Short.MAX_VALUE))
            .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lblPlaylists)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 211, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(lblActionButtons)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator2, javax.swing.GroupLayout.PREFERRED_SIZE, 2, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnCreation)
                    .addComponent(btnModification)
                    .addComponent(btnDeletion)
                    .addComponent(btnExportXML))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 2, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnExit)
                .addContainerGap())
        );

        bindingGroup.bind();

        pack();
        setLocationRelativeTo(null);
    }// </editor-fold>//GEN-END:initComponents

    private void btnExitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnExitActionPerformed
        try 
        {
            //this.prFrmRadioStation.setEnabled(true);
            General.closeWindowX(this);
        } 
        catch (Exception ex) 
        {
            JOptionPane.showMessageDialog(this, ex.getMessage(), this.getTitle(), JOptionPane.ERROR_MESSAGE);
        }

    }//GEN-LAST:event_btnExitActionPerformed

    private void btnCreationActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCreationActionPerformed
        try 
        {
            this.selectedPlaylist = new Playlist();
            this.selectedRow = -1;
            
            frmMngPlaylist = new MngPlaylist(this, this.selectedPlaylist, General.ActionType.Insert, this.em, this.username, this.selectedRow);
            //frmMngPlaylist.setVisible(true);
            //this.setEnabled(false);
            ApparentModalC.modalSetNextVisible(this, frmMngPlaylist);
                      
        } 
        catch (Exception ex) 
        {
            JOptionPane.showMessageDialog(this, ex.getMessage(), this.getTitle(), 0);
        }

    }//GEN-LAST:event_btnCreationActionPerformed

    private void btnModificationActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnModificationActionPerformed
        try 
        {
            this.selectedRow = this.playlistTable.getSelectedRow();
            
            if(this.selectedRow == -1)
            {
                JOptionPane.showMessageDialog(this, "Παρακαλώ επιλέξτε πρώτα την λίστα που επιθυμείτε να τροποποιήσετε!", this.getTitle(), JOptionPane.INFORMATION_MESSAGE);
            }
            else
            {
                selectedPlaylist = playlistList.get(playlistTable.convertRowIndexToModel(this.selectedRow));
                
                frmMngPlaylist = new MngPlaylist(this, selectedPlaylist, General.ActionType.Update, this.em, this.username, this.selectedRow);
               // frmMngPlaylist.setVisible(true);
               // this.setEnabled(false);
                ApparentModalC.modalSetNextVisible(this, frmMngPlaylist);

            }               

        } 
        catch (Exception ex) 
        {
            JOptionPane.showMessageDialog(this, ex.getMessage(), this.getTitle(), JOptionPane.ERROR_MESSAGE);
        }

    }//GEN-LAST:event_btnModificationActionPerformed

    private void btnDeletionActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDeletionActionPerformed
        try 
        {
            this.selectedRow = this.playlistTable.getSelectedRow();
            
            if(this.selectedRow == -1)
            {
                JOptionPane.showMessageDialog(this, "Παρακαλώ επιλέξτε πρώτα την λίστα που επιθυμείτε να διαγράψετε!", this.getTitle(), JOptionPane.INFORMATION_MESSAGE);
            }
            else
            {
                selectedPlaylist = playlistList.get(playlistTable.convertRowIndexToModel(this.selectedRow));
                
                frmMngPlaylist = new MngPlaylist(this, selectedPlaylist, General.ActionType.Delete, this.em, this.username, this.selectedRow);
                //rmMngPlaylist.setVisible(true);
                //this.setEnabled(false);
                ApparentModalC.modalSetNextVisible(this, frmMngPlaylist);

            }           

        } 
        catch (Exception ex) 
        {
            JOptionPane.showMessageDialog(this, ex.getMessage(), this.getTitle(), JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_btnDeletionActionPerformed

    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
        // TODO add your handling code here:
       // this.prFrmRadioStation.setEnabled(true);
    }//GEN-LAST:event_formWindowClosing

    private void btnExportXMLActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnExportXMLActionPerformed
        try 
        {
            // Get selected row
            this.selectedRow = this.playlistTable.getSelectedRow();
            
            if(this.selectedRow == -1)
            {
                // No row was selected
                JOptionPane.showMessageDialog(this, "Παρακαλώ επιλέξτε πρώτα την λίστα της οποίας τα στοιχεία επιθυμείτε να εξάγετε σε XML αρχείο!", this.getTitle(), JOptionPane.INFORMATION_MESSAGE);
            }
            else
            {
                // Get selected playlist
                selectedPlaylist = playlistList.get(playlistTable.convertRowIndexToModel(this.selectedRow));
                
                frmMngPlaylistXML = new MngPlaylistXML(this, null, selectedPlaylist, General.XMLActionType.Export, this.em, this.username);
                //frmMngPlaylistXML.setVisible(true);
                //this.setEnabled(false);
                 ApparentModalC.modalSetNextVisible(this, frmMngPlaylistXML);

            }           

        } 
        catch (Exception ex) 
        {
            JOptionPane.showMessageDialog(this, ex.getMessage(), this.getTitle(), JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_btnExportXMLActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCreation;
    private javax.swing.JButton btnDeletion;
    private javax.swing.JButton btnExit;
    private javax.swing.JButton btnExportXML;
    private javax.swing.JButton btnModification;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSeparator jSeparator2;
    private javax.swing.JLabel lblActionButtons;
    private javax.swing.JLabel lblPlaylists;
    public java.util.List<Model.Playlist> playlistList;
    public javax.persistence.Query playlistQuery;
    private javax.swing.JTable playlistTable;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration//GEN-END:variables
    @Override
    public Frame getCaller() {
        return this.prFrmRadioStation;
    }

    @Override
    public Frame getNext() {
        return this.next;
    }

    @Override
    public void setCallersNextNull() {
        ((ApparentModalI) this.prFrmRadioStation).setNext(null);
    }

    @Override
    public void setNext(Frame w) {
        this.next = (JFrame) w;
    }

}
